package com.example.electronicmall.base;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.electronicmall.R;
import com.example.electronicmall.adapter.ChoicepositionAapter;
import com.example.electronicmall.application.BaseApplication;
import com.example.electronicmall.bean.ChoicepositionBean;
import com.example.electronicmall.bean.ChoicepositionDataBean;
import com.example.electronicmall.bean.ChoicepositionDataListBean;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 作者：王辉
* 功能：我的收货地址
* 时间：2022/11/7
* */
public class ChoicepositionActivity extends AppCompatActivity {
   private final String TAG =ChoicepositionActivity.class.getCanonicalName();
   private List<ChoicepositionBean> list;
   private ChoicepositionBean bean;
   private ChoicepositionAapter aapter;
   private String province;//省
   private String city;//城市
   private String region; //区
   private String detailAddress;//街道

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choiceposition);
        initview();

    }
    private void initview(){
        RecyclerView recyclerView=findViewById(R.id.choiceposition_activity_recyclerview);
        list=new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        BaseApplication.request.requestshoppingactivity()
                .enqueue(new Callback<ChoicepositionDataBean>() {
                    @Override
                    public void onResponse(Call<ChoicepositionDataBean> call, Response<ChoicepositionDataBean> response) {
                       ChoicepositionDataListBean[] choice =response.body().getData();
                       //获取用户所有的收货地址
                        for (int i = 0; i < choice.length; i++) {
                            province=choice[i].getProvince();
                            city =choice[i].getCity();
                            region=choice[i].getRegion();
                            detailAddress=choice[i].getDetailAddress();
                             bean =new ChoicepositionBean();
                             bean.setChoicepositionName(choice[i].getName());
                             bean.setChoicepositionPhone(choice[i].getPhoneNumber());
                             bean.setChoicepositionPosition(province+city+region+detailAddress);
                             list.add(bean);
                        }
                        aapter=new ChoicepositionAapter(ChoicepositionActivity.this,list);
                        recyclerView.setAdapter(aapter);
                        position();
                    }

                    @Override
                    public void onFailure(Call<ChoicepositionDataBean> call, Throwable t) {
                               t.fillInStackTrace();
                        Log.d(TAG, "异常错误 "+t.fillInStackTrace());
                    }
                });

    }
    //选择收货地址
    private void position(){
        //选择收货地址
        aapter.setListener(new ChoicepositionAapter.OnClickExperintListener() {
            @Override
            public void onClickExperint(int position) {
                //带值回传
                Intent intent=new Intent();
                intent.putExtra("name",list.get(position).getChoicepositionName());
                intent.putExtra("phone",list.get(position).getChoicepositionPhone());
                intent.putExtra("position",list.get(position).getChoicepositionPosition());
                Log.d(TAG, "收货人姓名"+list.get(position).getChoicepositionName());
                Log.d(TAG, "收货人电话"+list.get(position).getChoicepositionPhone());
                Log.d(TAG, "收货人地址"+list.get(position).getChoicepositionPosition());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
}